package com.daffodil.workable.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.daffodil.core.annotation.AuthPermission;
import com.daffodil.core.annotation.DictConver;
import com.daffodil.core.annotation.Log;
import com.daffodil.core.annotation.Log.BusinessType;
import com.daffodil.core.controller.ReactiveBaseController;
import com.daffodil.core.entity.JsonResult;
import com.daffodil.core.entity.Page;
import com.daffodil.core.entity.TableInfo;
import com.daffodil.util.StringUtils;
import com.daffodil.workable.constant.WorkableConstant;
import com.daffodil.workable.entity.WorkableNode;
import com.daffodil.workable.service.IWorkableNodeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;
import reactor.core.publisher.Mono;

/**
 * -节点信息控制层
 * @author yweijian
 * @date 2022-04-07
 * @version 1.0
 * @description
 */
@Api(value = "节点信息管理", tags = "节点信息管理")
@RestController
@RequestMapping(WorkableConstant.API_CONTENT_PATH + "/node")
public class WorkableNodeController extends ReactiveBaseController {

    @Autowired
    private IWorkableNodeService workableNodeService;

    @ApiOperation("分页查询节点信息列表")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @SuppressWarnings("unchecked")
    @DictConver
    @AuthPermission("workable:node:list")
    @GetMapping("/list")
    public Mono<TableInfo> list(WorkableNode workableNode, Page page, @ApiIgnore ServerHttpRequest request) {
        initQuery(workableNode, page, request);
        List<WorkableNode> list = workableNodeService.selectWorkableNodeList(query);
        return initTableInfo(list, query).getMono();
    }

    @ApiOperation("获取节点信息详情")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @GetMapping("/info")
    public Mono<JsonResult> info(@ApiParam(value = "节点信息ID") String id, 
            @ApiParam(value = "计划任务ID") String taskId, 
            @ApiParam(value = "节点编号ID") String nodeId, 
            @ApiIgnore ServerHttpRequest request) {
        if(StringUtils.isNotEmpty(id)) {
            WorkableNode workableNode = workableNodeService.selectWorkableNodeById(id);
            return JsonResult.success(workableNode).getMono();
        }
        WorkableNode workableNode = workableNodeService.selectWorkableNodeByTaskIdAndNodeId(taskId, nodeId);
        return JsonResult.success(workableNode).getMono();
    }

    @ApiOperation("新增节点信息")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("workable:node:add")
    @Log(title = "节点信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Mono<JsonResult> add(@Validated @RequestBody WorkableNode workableNode, @ApiIgnore ServerHttpRequest request) {
        workableNodeService.insertWorkableNode(workableNode);
        return JsonResult.success(workableNode).getMono();
    }

    @ApiOperation("修改节点信息")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("workable:node:edit")
    @Log(title = "节点信息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Mono<JsonResult> edit(@Validated @RequestBody WorkableNode workableNode, @ApiIgnore ServerHttpRequest request) {
        workableNodeService.updateWorkableNode(workableNode);
        return JsonResult.success(workableNode).getMono();
    }

    @ApiOperation("删除节点信息")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("workable:node:remove")
    @Log(title = "节点信息管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public Mono<JsonResult> remove(@RequestBody String[] ids, @ApiIgnore ServerHttpRequest request) {
        workableNodeService.deleteWorkableNodeByIds(ids);
        return JsonResult.success().getMono();
    }

}
